/**
 * @author huangmy
 * @text   工号管理
 */

Ext.extend(CQCIS.module, {
    init: function() {
    	// 页面列表
        var listMeta = [{
            id: 'loginId',
            field: 'loginId',
            desc: 'ID',
            hidden: true
        }, {
            field: 'loginCode',
            desc: '登录工号',
            width: 100
        }, {
        	field: 'staffId',
        	desc: '员工名称',
        	renderer: function(value) {
                staffDwr.getStaff(value, {
                    callback: function(region) {
                        if (region) {
                            value = region.staffName;
                        } else {
                            value = '';
                        }
                    },
                    async: false
                });
                
                return value;
            },
        	width: 100
        }, {
            field: 'loginPwd',
            desc: '登录密码',
            width: 100
        }, {
            field: 'loginType',
            desc: '登录级别',
            width: 100,
            value: 0,
            hidden: true
        }, {
            field: 'sendPhone',
            desc: '发送短信号码',
            width: 100
        }, {
            field: 'loginNum',
            desc: '登录次数',
            width: 100
        }, {
            field: 'lastChgPwdTime',
            desc: '最后一次修改密码时间',
            type: 'date',
            width: 100
        }, {
            field: 'state',
            desc: '状态',
            width: 60,
            renderer: function(value) {
                var str = "";
                switch (value) {
                    case '1':
                        str = "有效";
                        break;
                    default:
                        str = "无效";
                        break;
                }
                return str;
            }
        }];
        
        // 增加
        var workerMeta = [{
            id: 'loginId',
            name: 'loginId',
            fieldLabel: 'ID',
            xtype: 'hidden'
        }, {
            name: 'loginCode',
            fieldLabel: '登录工号',
            allowBlank: false,
            regex: /^[A-Za-z0-9]+$/,
            regexText: '工号只能由英文或数字组成'
        }, {
            name: 'staffId',
            fieldLabel: '员工ID',
            xtype: 'hidden'
        }, {
            name: 'loginPwd',
            fieldLabel: '登录密码',
            regex: /^[A-Za-z0-9]+$/,
            regexText: '工号只能由英文或数字组成'
        }, {
            name: 'loginType',
            fieldLabel: '登录级别',
            xtype: 'hidden'
        }, {
            name: 'sendPhone',
            fieldLabel: '发送短信号码',
            regex: /^(\d{8,8}|\d{11,11})$/,
            regexText: '电话8位或11位'
        }, {
            name: 'loginNum',
            fieldLabel: '登录次数',
            xtype: 'hidden'
        }, {
            name: 'lastChgPwdTime',
            fieldLabel: '最后一次修改密码时间',
            type: 'date',
            xtype: 'hidden'           
        }, {
            fieldLabel: '状态',
            name: 'state',
            store: [['1', '有效'], ['0', '无效']],
            triggerAction: 'all',
            mode: 'local',
            allowBlank: false,
            editable: false,
            xtype: 'combo',
            value: '1'
        }];
        
        // 员工信息窗口
        var workerWin = new CQCIS.Window({
            id: 'sm_worker_teamWin',
            title: '工号信息',
            height: 200,
            items: new CQCIS.form.DWRForm({
                labelWidth: 100,
                dwr: {
                    load: workerDwr.getWorker,
                    save: workerDwr.createWorker,
                    update: workerDwr.updateWorker
                },
                items: workerMeta,
                afterSubmit: function(teamId) {
                    grid.refresh();
                }
            })
        });
        
        var searchName = new Ext.form.TextField({
        });
        
        var searchBtn = new Ext.Toolbar.Button({
            text: '查询',
            iconCls: 'btn_insert',
            handler: function() {
                var loginCode = searchName.getValue();
                
                grid.like['loginCode'] = loginCode;
                
                grid.reload();
            }
        });
        
        // 工具栏修改按钮
        var editBtn = new Ext.Toolbar.Button({
            text: '修改',
            iconCls: 'btn_modify',
            handler: function() {
                var menu = grid.getSelectedRow();
                if (menu) {
                    workerWin.load(menu.loginId);
                } else {
                    CQCIS.msg('请选择要修改的记录!');
                }
            }
        });
        
        // 工具栏关联角色按钮
        var roleBtn = new Ext.Toolbar.Button({
            text: '关联角色',
            iconCls: 'btn_config',
            handler: function() {
                var menu = grid.getSelectedRow();
                if (menu) {
                    //workerWin.load(menu.loginId);
                } else {
                    CQCIS.msg('请选择要关联的工号!');
                }
            }
        });
        
        // 工具栏权限微调按钮
        var configBtn = new Ext.Toolbar.Button({
            text: '权限微调',
            iconCls: 'btn_config',
            handler: function() {
                var menu = grid.getSelectedRow();
                if (menu) {
                    //workerWin.load(menu.loginId);
                } else {
                    CQCIS.msg('请选择要微调的工号!');
                }
            }
        });
                        
        // 员工信息grid
        var grid = new CQCIS.grid.PageGrid({
            id: 'sm_worker_grid',
            firstColumn: 'radio',
            dwr: {
                call: workerDwr.pageWorkers
            },
            metas: listMeta,
            tbar: ['请输入登录工号:', searchName, searchBtn, editBtn, roleBtn, configBtn],
            bbar: 'true',
            initLoad: true
        });
        
        // 定义关联销毁对象数组，避免内存泄漏
        this.main.associatedComp = [workerWin];
        // 定义页面主元素
        this.main.add(grid);
        // 布局更新
        this.main.doLayout();
    }
});
